//
//  Me2DayUtil.m
//  CairnStory
//  www.cairnstory.com
//
//  Created by saturna on 11. 3. 22..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Me2DayUtil.h"
#import <MobileCoreServices/MobileCoreServices.h>


@implementation Me2DayUtil


- (void)dealloc
{
	[super dealloc];
}


+ (NSMutableDictionary*)parseQueryString:(NSString*)queryString
{
    NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
    NSArray* pairs = [queryString componentsSeparatedByString:@"&"];

    for (NSString* pair in pairs)
	{
        NSArray* elements = [pair componentsSeparatedByString:@"="];
        NSString* key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString* val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        [dict setObject:val forKey:key];
    }
    return [dict autorelease];
}


+ (NSString*)formatDate:(NSDate*)date style:(NSString*)formatStyle
{
	NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
	[df setDateFormat:formatStyle];
	return [df stringFromDate:date];
}

+ (NSString*)formatDateSince1970:(double)since1970 style:(NSString*)formatStyle
{
	NSDate *aDate = [NSDate dateWithTimeIntervalSince1970:since1970];
	return [self formatDate:aDate style:formatStyle];
}

@end





#pragma mark -

NSString* GetMimeType(NSString* fileName)
{
	CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
															(CFStringRef)[fileName pathExtension],
															NULL);
	CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
	CFRelease(UTI);
	if (MIMEType == nil)
	{
		//NSString* extension = [[fileName pathExtension] lowercaseString];
		// user defined mime type
		// caf, kml, kmz....
		return @"application/octet-stream";
	}

	return [(NSString *)MIMEType autorelease];
}

BOOL EmptyString(NSString* value)
{
	return value == nil || [value isKindOfClass:[NSNull class]] || [value isEqualToString:@""];
}


